/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.util.ArrayList;
import models.memberships.AbstractMembership;
import models.roles.AbstractRole;

/**
 *
 * @author pelle
 */
public class Person {
    private int id;
    private String name = "";
    private String companyName = "";
    private String address = "";
    private int zipcode;
    private String city = "";
    private String email = "";
    private String phone = "";
    private String cpr = "";
    
    private String password = "";
    
    private AbstractMembership membership = null;   

    private ArrayList<Person> contacts = new ArrayList<Person>();
    private ArrayList<AbstractRole> personRoles = new ArrayList<AbstractRole>();

    public Person() {
        
    }
    
    // Person uden et ID (fx en ny person)
    public Person(String name, String address, int zipcode, String city, String email, String phone, String cpr) {
        this.name = name;
        this.address = address;
        this.zipcode = zipcode;
        this.city = city;
        this.email = email;
        this.cpr = cpr;
        this.phone = phone;
    }

    // Firma/sponsor uden ID (fx en ny)
    public Person(String name, String companyName, String address, int zipcode, String city, String email, String phone, String cpr) {
        this.name = name;
        this.companyName = companyName;
        this.address = address;
        this.zipcode = zipcode;
        this.city = city;
        this.email = email;
        this.cpr = cpr;
        this.phone = phone;
    }

    // Person med et ID
    public Person(int id, String name, String address, int zipcode, String city, String email, String phone, String cpr) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.zipcode = zipcode;
        this.city = city;
        this.email = email;
        this.cpr = cpr;
        this.phone = phone;
    }
    
    // Person med ID, men uden by
    public Person(int id, String name, String address, int zipcode, String email, String phone, String cpr) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.zipcode = zipcode;
        this.phone = phone;
        this.email = email;
        this.cpr = cpr;
    }
    
    // Firma/sponsor med ID
    public Person(int id, String name, String companyName, String address, int zipcode, String city, String email, String phone, String cpr) {
        this.id = id;
        this.name = name;
        this.companyName = companyName;
        this.address = address;
        this.zipcode = zipcode;
        this.city = city;
        this.email = email;
        this.cpr = cpr;
        this.phone = phone;
    }
    
    // Firma/sponsor med ID, men uden by
    public Person(int id, String name, String companyName, String address, int zipcode, String email, String phone, String cpr) {
        this.id = id;
        this.name = name;
        this.companyName = companyName;
        this.address = address;
        this.zipcode = zipcode;
        this.email = email;
        this.cpr = cpr;
        this.phone = phone;
    }

    private boolean isInRoleList(String type) {
        boolean found = false;
        type = type.toLowerCase();

        // Løber alle rollerne til personen igennem
        for(AbstractRole role : personRoles) {

            // Eksempel: model.roles.Inspector
            String roleType = role.getClass().getName();
            
            // Skærer stengen ud ved det sidste punktum, så det kun er "Inspector" der er tilbage
            // og gør det derefter til små bogstaver
            roleType = roleType.substring(roleType.lastIndexOf(".")+1).toLowerCase();

            // Ser om det er den rolle vi egentlig leder efter
            if(roleType.equals(type)) {
                found = true;
                break;
            }
        }
        return found;
    }

    // Metode til at beslutte om man må logge ind i systemet eller ej
    public boolean isAllowedToLogin() {
        if(this.isInspector() || this.isManager() || this.isTrainer()) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isInspector() {
        return this.isInRoleList("Inspector");
    }

    public boolean isManager() {
        return this.isInRoleList("Manager");
    }

    public boolean isMember() {
        return this.isInRoleList("Member");
    }

    public boolean isSponsor() {
        return this.isInRoleList("Sponsor");
    }

    public boolean isTrainer() {
        return this.isInRoleList("Trainer");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the companyName
     */
    public String getCompanyName() {
        return companyName;
    }

    /**
     * @param companyName the companyName to set
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the zipcode
     */
    public int getZipcode() {
        return zipcode;
    }

    /**
     * @param zipcode the zipcode to set
     */
    public void setZipcode(int zipcode) {
        this.zipcode = zipcode;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
    
    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the cpr
     */
    public String getCpr() {
        return cpr;
    }

    /**
     * @param cpr the cpr to set
     */
    public void setCpr(String cpr) {
        this.cpr = cpr;
    }
    
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the contacts
     */
    public ArrayList<Person> getContacts() {
        return contacts;
    }

    /**
     * @param contacts the contacts to set
     */
    public void setContacts(ArrayList<Person> contacts) {
        this.contacts = contacts;
    }

    // Metode til at tilføje en relateret person
    public boolean addContact(Person person) {
        boolean found = false;
        
        // Looper alle de nuværende personer igennem
        for(Person p : this.getContacts()) {
            
            // Tjekker om den person vi vil tilføje allerede findes
            if(person.getId() == p.getId()) {
                
                // Hvis han/hun er der, returnere vi true, for så har vi jo i teorien tilføjet ham/hende alligevel
                found = true;
                
                // Stopper loopet. Ingen grund til at køre mere.
                break;
            }
        }
        
        // Der blev ikke fundet, så tilføjer vi personen
        if(!found) {
            found = this.contacts.add(person);
        }
        
        return found;
    }
    
    // Fjerne en relateret person
    public boolean removeContact(Person person) {
        boolean success = false;
        
        // Løber alle personerne igennem
        for(Person p : this.getContacts()) {
            
            // Tjekker om ID'et passer
            if(person.getId() == p.getId()) {
                
                // Fjerner personen fra arraylisten
                if(this.contacts.remove(p)) {
                    success = true;
                    
                    // Stopper loopet. Ingen grund til at køre mere.
                    break;
                }
            }
        }
        
        return success;
    }

    /**
     * @return the personRoles
     */
    public ArrayList<AbstractRole> getPersonRoles() {
        return personRoles;
    }

    /**
     * @param personRoles the personRoles to set
     */
    public void setPersonRoles(ArrayList<AbstractRole> personRoles) {
        this.personRoles = personRoles;
    }

    public void addPersonRole(AbstractRole role) {
        this.personRoles.add(role);
    }
}
